﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PaulBeck.Timesheet.BOL
{



    public struct BookTimeItemResourceProject
    {
        public double? Time { get; set; }
        public int ProjectResourceId { get; set; }
        public int? DayId { get; set; }
        public string Notes { get; set; }
        public int ProjectId { get; set; }
        public int ResourceId { get; set; }
    }


    public struct BookTimeItemResourceDays
    {
        public DateTime DayDate { get; set; }
        public int DayEntryId { get; set; }
    }

    public struct BookTimeItemProjectInfo
    {
        public int ProjectResourceId { get; set; }
        public int ProjectId { get; set; }
        public string ProjectDescription { get; set; }
    }

    public static class BookTimeItemBOL
    {

        public static List<BookTimeItemProjectInfo> BookTimeItemGetProjectInfo(int userId)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            var projects = (from pr in dc.ProjectResources
                            where pr.ResourceId == userId
                            select new BookTimeItemProjectInfo
                            {
                                ProjectResourceId = pr.ProjectResourceId,
                                ProjectId = pr.ProjectId,
                                ProjectDescription = pr.Project.Description,
                            });
            return projects.ToList();
        }


        public static List<BookTimeItemResourceDays> BookTimeItemGetResourceDays(int weekId)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            var days = (from d in dc.DayEntries
                        where d.WeekEntryId == weekId
                        select new BookTimeItemResourceDays
                        {
                            DayDate = d.DayDate,
                            DayEntryId = d.DayEntryId,
                        });
            return days.ToList();
        }

        public static BookTimeItemResourceProject BookTimeItemGetResource(int rptid)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            var resource = (from rpt in dc.ResourceProjectTimes
                            where rpt.ResourceProjectTimeId == rptid
                            select new BookTimeItemResourceProject
                            {
                                Time = rpt.Time,
                                ProjectResourceId = rpt.ProjectResourceId,
                                DayId = rpt.DayEntryIdFk,
                                Notes = rpt.Notes,
                                ProjectId = rpt.ProjectResource.ProjectId,
                                ResourceId = rpt.ProjectResource.ResourceId,
                            }).Single();
            return resource;
        }

        public static int BookTimeItemGetResourceId(string loginName)
        {
            int userId = 0;
            try
            {
                TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                var user = (from res in dc.Resources
                            where res.Name == loginName
                            select new
                            {
                                res.ResourceId,
                            }).Single();
                userId = user.ResourceId;
            }
            catch (System.InvalidOperationException ex)
            {

            }
            return userId;
        }

        public static ResourceProjectTime BookTimeItemDelete(int rptid)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            ResourceProjectTime rpt = dc.ResourceProjectTimes.Single(r => r.ResourceProjectTimeId == rptid);
            dc.ResourceProjectTimes.DeleteOnSubmit(rpt);
            dc.SubmitChanges();
            return rpt;
        }

        public static ResourceProjectTime BookTimeItemInsert(string loginName, int day, string notes, int project, double time)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            ResourceProjectTime rpt = new ResourceProjectTime();
            rpt.CreatedBy = loginName;
            rpt.CreatedDate = DateTime.Now;
            rpt.DayEntryIdFk = day;
            rpt.Notes = notes;
            rpt.ProjectResourceId = project;
            rpt.Time = time;
            dc.ResourceProjectTimes.InsertOnSubmit(rpt);
            dc.SubmitChanges();
            return rpt;
        }

        public static ResourceProjectTime BookTimeItemUpdate(int rptid, string notes, int day, int projectResourceID, double time)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            ResourceProjectTime rpt = dc.ResourceProjectTimes.Single(r => r.ResourceProjectTimeId == rptid);
            string loginName = CommonWFE.CurrentUserName();
            rpt.LastUpdatedBy = loginName;
            rpt.LastUpdatedDate = DateTime.Now;
            rpt.Notes = notes;
            rpt.DayEntryIdFk = day;
            rpt.ProjectResourceId = projectResourceID;
            rpt.Time = time;
            dc.SubmitChanges();
            return rpt;
        }

        public static void UpdateDayTime(int dayEntryId)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            var spResult = dc.sp_ResourceProjectTimeSumTime(dayEntryId).FirstOrDefault();
            if (spResult != null)
            {
                DayEntry day = dc.DayEntries.Single(d => d.DayEntryId == dayEntryId);
                day.Time = spResult.Column1;
                dc.SubmitChanges();
                //Update the Week Time column total
                WeekEntry week = dc.WeekEntries.Single(w => w.WeekEntryId == day.WeekEntryId);
                var spResultWeek = dc.sp_GetTimeforWeekUsingDays(week.WeekEntryId).FirstOrDefault();
                if (spResultWeek != null)
                {
                    week.Time = spResultWeek.Column1;
                    dc.SubmitChanges();
                }
            }
        }

        public static void UpdateProjectResourceTime(int projectResourceId)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            var spResultProjectResource = dc.sp_ResourceProjectTimeSumTimeByResource(projectResourceId).FirstOrDefault();
            if (spResultProjectResource != null)
            {
                ProjectResource prjRes = dc.ProjectResources.Single(pr => pr.ProjectResourceId == projectResourceId);
                prjRes.TotalAllocatedHours = (decimal)spResultProjectResource.Column1;
                prjRes.LastUpdatedBy = CommonWFE.CurrentUserName();
                prjRes.LastUpdatedDate = DateTime.Now;
                dc.SubmitChanges();
            }
        }

    }
}
